<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/12/28  16:25
 */
namespace app\admin\controller;
use app\admin\controller\Export;
use think\Db;

class Driver extends Common{

    /*
    *	构造函数
    */
    function __construct(){
        parent::__construct();

    }

    /**
     * 司机列表
     */
    public function index(){
        $status=input("status");
        if($status!=''){
            $where['status']=$status;
        }else{
            $where['status']=array('gt',-1);
        }
        if(input("keyword")){
            $where['nickname|mobile']=array("like",'%'.input('keyword').'%');
        }
        if(input("export")){
            $this->export($where);exit;
        }
        $list = db("driver")->where($where)->order("id desc")->paginate(10);
        $count = db("driver")->where($where)->order("id desc")->count();
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('status',input("status"));
        $this->assign('type',input("type"));
        $this->assign('keyword',input("keyword"));
        return $this->fetch();
    }

    /*
     * 司机详情
     */
    public function detail()
    {
        $id = input('id');
        $res = db('driver')->where(['id'=>$id])->find();
//        halt($res);
        $this->assign('data', $res);
        return $this->fetch();
    }


    /*
     * 设置路线
     */
    public function setline()
    {
        $id = input('id/d');
        if(request()->isAjax()){
            $data['line'] = input('line/s');
            $res = db('driver')->where(['id'=>$id])->update($data);
            if($res){
                $this->success('修改成功！');
            }else{
                $this->error('修改失败！');
            }
        }else{
            $driver = db('driver')->where(['id'=>$id])->find();
            $this->assign('data', $driver);
            $route = db('route')->field('route_sn')->group('route_sn')->select();
            $this->assign('route', $route);
            return $this->fetch();
        }

    }

    /*
    审核司机
    */
    public function check()
    {
        $id = input('id/d');
        $status=input("status/d");
        if(!in_array($status,array(0,1,-1))){
            exit(json_encode('参数有误'));
        }
        $db=db("driver");
        $data['islicense']=$status;
        $driver=$db->where("id='$id'")->find();
        if($status==1) {
            if ($driver['status'] == 0 || $driver['status'] == -1) {
                exit(json_encode('请先审核司机的状态'));
            }
        }
        $res=$db->where("id='$id'")->update($data);
        if($res){
            exit(json_encode(1));
        }else{
            exit(json_encode(0));
        }
    }

    /**setagentblack
     *   搜索会员
     */
    public function sou(){
        $key=input("key");
        $where['nickname|mobile']=array("like",'%'.$key.'%');
        $where['status']=1;
        $where['type']=1;
        $data=db("driver")->where($where)->select();
        $str="";
        foreach($data as $v){
            $str.= "<option value='".$v['id']."'>".$v['nickname'].'-'.$v['mobile']."</option>";
        }
        echo $str;
    }
    /**
     *   黑名单
     */

    /**
     * 会员列表导出excel
     */
    public function export($where)
    {

        $headArr = array("司机ID","司机昵称","手机号码","openid","注册日期","地址");
        $list = db('driver')
            ->where($where)
            ->select();
        if($list){
            $data=array();
            foreach($list as $k => $v){
                $data[$k][] = $v['id'];
                $data[$k][] = $v['nickname'];
                $data[$k][] = $v['mobile'];
                $data[$k][] = $v['wx_openid'];
                $data[$k][] = date("Y-m-d H:i",$v['create_time']);
                $data[$k][] = $v['address'];
            }
            $filename='司机列表';
            (new Export())->ImportExcel($filename,$headArr,$data);
        }
    }

    protected function column_str($key)
    {
        $array = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ', 'BA', 'BB', 'BC', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BK', 'BL', 'BM', 'BN', 'BO', 'BP', 'BQ', 'BR', 'BS', 'BT', 'BU', 'BV', 'BW', 'BX', 'BY', 'BZ', 'CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CI', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CQ', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ');
        return $array[$key];
    }

    protected function column($key, $columnnum = 1)
    {
        return $this->column_str($key) . $columnnum;
    }
    /*
  * 设置黑名单
  */
    public function setblack(){
        $id = input('id/d');
        $member=db("driver")->where("id='$id'")->find();
        if($member['status']==0){
            $data['status']=1;
        }else{
            $data['status']=0;
        }
        $res=db("driver")->where("id='$id'")->update($data);
        if($res){
            exit(json_encode(1));
        }else{
            exit(json_encode(0));
        }
    }

    /*
* 删除
*/
    public function del(){
        $id = input('id/d');
        $data['status'] =-1;
        $res=db("driver")->where("id='$id'")->update($data);
        if($res){
            echo 1;exit;
        }else{
            echo -1;exit;
        }
    }

    /*
 * 线路 列表
 */
    public function route()
    {
        $data = db('route')->order('id desc')->field('id,route_sn')->select();
//		var_dump($data);
        foreach($data as $key=>&$value){
            $address = db('route_address')->where('route_sn = '."'".$value['route_sn']."'")->column('address');
            $data[$key]['address'] = implode('-',$address);
//			$data[$key]['address'] = $address;
        }
//		halt($data);
        $this->assign('route', $data);
        return $this->fetch();
    }

    public function routeedit()
    {
        $id = input('id');
        if($id){
            $this->assign('id', input('id'));
            $route_sn = db('route')->where('id='.$id)->value('route_sn');
            $route_address = db('route_address')->where(['route_sn'=>$route_sn])->select();
//			var_dump($route_address);
            $this->assign('route_address', $route_address);
        }
        return $this->fetch();
    }

    public function do_routeedit()
    {
        $id = input('id');
        if(request()->isAjax()){
            $specs = input('specs');
            $specs = json_decode($specs,1);
            if(!empty($id)){
                $route_sn = db('route')->where(['id'=>$id])->value('route_sn');
            }else{
                $route_sn = $this->becomerouteNo();
            }
            $data = [];
            foreach($specs as $key=>&$value){
                $data[$key]['lat'] = $value['lat'];
                $data[$key]['lng'] = $value['lng'];
                $data[$key]['address'] = $value['address'];
                if(!empty($data[$key]['address'])){
                    $data[$key]['route_sn'] = '';
                    $url = 'http://api.map.baidu.com/cloudgc/v1?address='.$data[$key]['address'].'&ak=76cuHriEX1uN5yXu899NAKBWqVVYooxm';
                    $res = file_get_contents($url);
                    $res = json_decode($res,true);
                    if($res['status'] == 0){
                        $lat = $res['result'][0]['location']['lat'];
                        $lng = $res['result'][0]['location']['lng'];
                    }
                    $data[$key]['lng'] = isset($lng)?$lng:'';
                    $data[$key]['lat'] = isset($lat)?$lat:'';
                    $data[$key]['route_sn'] = $route_sn;
                }
            }
            if(!empty($id)){
                //修改
                if(db('route_address')->where(['route_sn'=>$route_sn])->delete()){$res = db('route_address')->insertAll($data);}

            }else{
                //添加
                $route['route_sn'] = $route_sn;
                if(db('route')->insert($route)){$res = db('route_address')->insertAll($data);}

            }
            if($res)$this->success('编辑成功');
            $this->error('编辑失败');
        }
    }

    public function routedetail()
    {
        $data['route_sn'] = input('sn');

        $address = db('route_address')->where('route_sn = '."'".$data['route_sn']."'")->column('address');
        $data['address'] = implode('-',$address);
        $this->assign('data', $data);
        return $this->fetch();
    }
    public static function becomerouteNo()
    {
        $yearCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
        $routeSn =
            $yearCode[intval(date('Y')) - 2017] . time();
        return $routeSn;
    }


    public function optiontpl()
    {
        return $this->fetch();
    }
}